package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.Printer;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.PrinterDataHandler;
import com.efast.dmom.mes.service.PrinterService;

@Service
public class PrinterServiceImpl  implements PrinterService{
	
	@Autowired
	private PrinterDataHandler printerHandler;
	@Override
	public ResultBean save(Printer printer) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if(printer==null || printer.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String name = printer.getInnerData().getName();
		long plantId = printer.getInnerData().getPlantId();
		if("".equals(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("打印机名称为必填项");
			return resultBean;
		}
		if(plantId==0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工厂为必填项");
			return resultBean;
		}
		Printer printer2 = printerHandler.getByNameAndPlantId(name, plantId);
		if(printer2.getInnerData() !=null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("打印机名称："+name+" 已经存在");
			return resultBean;
		}
		printerHandler.save(printer);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功");
		return resultBean;	
	}

	@Override
	public ResultBean update(Printer printer) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true, ' ');
		if(printer==null || printer.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String name = printer.getInnerData().getName();
		long plantId = printer.getInnerData().getPlantId();
		if("".equals(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("打印机名称为必填项");
			return resultBean;
		}
		if(plantId==0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工厂为必填项");
			return resultBean;
		}
		Printer printer2 = printerHandler.getById(printer.getInnerData().getId());
		if(printer2.getInnerData() ==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		if(!printer2.getInnerData().getName().equals(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("打印机名称不可编辑!");
			return resultBean;
		}
		printerHandler.update(printer);
		
		resultBean.setSuccess(true);
		resultBean.setMessage("编辑成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(Printer printer) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if(printer==null || printer.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		printerHandler.delete(printer);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功");
		return resultBean;
	}

	@Override
	public ResultBean batchDelete(List<Printer> listPrinter) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true, ' ');
		if(listPrinter!=null && listPrinter.size()>0) {
			printerHandler.batchDelete(listPrinter);
		}
		resultBean.setMessage("删除成功");
		return resultBean;
	}

}
